package pk.nak.client.activity;

import pk.nak.client.R;
import pk.nak.enums.AppVars;
import pk.nak.requests.LoginRequest;
import pk.nak.responses.LoginResponse;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

	/** Called when the activity is first created. */
	private LoginResponse response = new LoginResponse(){

		@Override
		public void onSuccess(String response) {
			// TODO Auto-generated method stub
			
			super.onSuccess(response);
			
			AppVars.pdWait.dismiss();
			
			if ( this.getVerified() ){
				AppVars.LogedInUserID = this.getUserID();
				Intent mainIntent = new Intent(getApplicationContext(), NearestEatoutActivity.class);
				startActivity(mainIntent);
			}else{
				Toast.makeText(getApplicationContext(), "Invalid Login", Toast.LENGTH_SHORT).show();
			}
			
		}
	};
	
	private View.OnClickListener btnLogin_OnClick = new View.OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			EditText etEmail = (EditText) findViewById(R.id.etEmail);
			EditText etPass = (EditText) findViewById(R.id.etPassword);
			
			String user_email = "";
			String user_pass = "";
			
			if ( etEmail != null ) user_email = etEmail.getText().toString();
			if ( etPass != null ) user_pass = etPass.getText().toString();
			
			LoginRequest request = new LoginRequest(user_email, user_pass);
			request.doRequst(response);
			AppVars.pdWait = ProgressDialog.show(v.getContext(), "Verifying User", "Please Wait");
			
			/*
			Intent mainIntent = new Intent(getApplicationContext(), NearestEatoutActivity.class);
			startActivity(mainIntent);
			*/		
			
		}
	};
	
	private View.OnClickListener btnSignUp_OnClick = new View.OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent signupIntent = new Intent(v.getContext(),SignUpActivity.class);
			startActivity(signupIntent);
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.login);
	    this.attachHandlers();
	    // TODO Auto-generated method stub
	}
	
	private void attachHandlers(){
		Button btnLogin = (Button) findViewById(R.id.btnLogin);
		if ( btnLogin != null ) btnLogin.setOnClickListener(btnLogin_OnClick);
		
		Button btnSignUp = (Button) findViewById(R.id.btnSignUp);
		if (btnSignUp != null ) btnSignUp.setOnClickListener(btnSignUp_OnClick);
	}
}
